package calendar_ex;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Scanner;

public class CalendarDate {

	// class to store a date for use with the calendar
	// NOTE: Only accurate until the next leap year.

	// constants to provide an initial day for the date id
	private static int FIRST_DAY = 1, FIRST_MONTH = 1, FIRST_YEAR = 2009;

	// day, month and year of the Date
	public int day, month, year;

	// unique id given from the date. Counting from FIRST_DAY, FIRST_MONTH, FIRST_YEAR
	int dateID;
	
	// method to return current system time in form dd/MM/yy
	public String getSysDate(){
		
		Calendar currentDate = Calendar.getInstance();
		SimpleDateFormat formatter= new SimpleDateFormat("dd/MM/yy");
		String dateNow = formatter.format(currentDate.getTime());
		String s = dateNow;
		return s;
		
	}
	
	//default constructor uses current system date
	public CalendarDate(){
		Scanner defaultScanner = new Scanner(getSysDate());
		defaultScanner.useDelimiter("/");
		day = defaultScanner.nextInt();
		month = defaultScanner.nextInt();
		year = defaultScanner.nextInt();
	}

	public CalendarDate(int the_day, int the_month, int the_year){
		day = the_day;
		month = the_month;
		year = the_year;

		dateID = getDateID(day, month, year);
	}

	public CalendarDate(int id){

		CalendarDate cd = getDateFromID(id);
		day = cd.day;
		month = cd.month;
		year = cd.year;

		dateID = id;
	}

	public static int getDateID(CalendarDate cd){
		// from the date, get the appropriate date ID
		return getDateID(cd.day, cd.month, cd.year);
	}

	public static int getDateID(int d, int m, int y){
		// get the date ID from the day, month and year

		if(d>31 || d<1 || m>12 || m<1 || y < FIRST_YEAR)
			return -1;

		int no_years = y - FIRST_YEAR;
		int no_months = m - FIRST_MONTH;
		int no_days = d - FIRST_DAY;

		int y_val = no_years * 365;
		int m_val = 0;

		switch (no_months){
		case 0: m_val = 0; break;
		case 1: m_val = 31;break;
		case 2: m_val = 59;break;
		case 3: m_val = 90;break;
		case 4: m_val = 120;break;
		case 5: m_val = 151;break;
		case 6: m_val = 181;break;
		case 7: m_val = 212;break;
		case 8: m_val = 243;break;
		case 9: m_val = 273;break;
		case 10: m_val = 304;break;
		case 11: m_val = 334;break;

		default: return -1;

		}

		int d_val = no_days;

		return y_val + m_val + d_val;
	}

	public static CalendarDate getDateFromID(int id){
		// given a date id, get the relevant calendar date
		if(id < 0)
			return null;

		int no_years = id / 365;
		int no_months =0;
		int no_days = 0;

		int rem_month_id = id - (no_years* 365);

		if(rem_month_id < 31){
			no_months = 1;
			no_days = 1 + rem_month_id;
		}
		else if(rem_month_id < 59){
			no_months = 2;
			no_days = rem_month_id-30;
		}
		else if(rem_month_id < 90){
			no_months = 3;
			no_days = rem_month_id-58;
		}
		else if(rem_month_id < 120){
			no_months = 4;
			no_days = rem_month_id-89;
		}
		else if(rem_month_id < 151){
			no_months = 5;
			no_days = rem_month_id-119;
		}
		else if(rem_month_id < 181){
			no_months = 6;
			no_days = rem_month_id-150;
		}
		else if(rem_month_id < 181){
			no_months = 6;
			no_days = rem_month_id-150;
		}
		else if(rem_month_id < 212){
			no_months = 7;
			no_days = rem_month_id-180;
		}
		else if(rem_month_id < 243){
			no_months = 8;
			no_days = rem_month_id-211;
		}
		else if(rem_month_id < 273){
			no_months = 9;
			no_days = rem_month_id-242;
		}
		else if(rem_month_id < 304){
			no_months = 10;
			no_days = rem_month_id-272;
		}
		else if(rem_month_id < 334){
			no_months = 11;
			no_days = rem_month_id-303;
		}
		else{
			no_months = 12;
			no_days = rem_month_id-333;
		}

		return new CalendarDate(no_days, no_months, no_years+FIRST_YEAR);

	}

	public static String getDay(int d, int m, int y){
		// Given the date, get the day

		int the_id = getDateID(d, m, y);

		switch (the_id % 7){
		case 0 : return "THURSDAY";
		case 1 : return "FRIDAY";
		case 2 : return "SATURDAY";
		case 3 : return "SUNDAY";
		case 4 : return "MONDAY";
		case 5 : return "TUESDAY";
		case 6 : return "WEDNESDAY";
		default: return "ANYDAY";
		}
	}
	public static int getDayInt(int d, int m, int y){
		// Given the date, get the day

		int the_id = getDateID(d, m, y);

		switch (the_id % 7){
		case 0 : return 1;
		case 1 : return 2;
		case 2 : return 3;
		case 3 : return 4;
		case 4 : return 5;
		case 5 : return 6;
		case 6 : return 7;
		default: return 0;
		}
	}
	public static CalendarDate nextDay(CalendarDate d){
		int day,month,year;
		day=d.day;
		month=d.month;
		year=d.year;
		int daysInMonth = d.getNoDays(month);
		if(day==daysInMonth){
			day=1;
			if(month==12){
				month=1;
				year++;
			}
			month++;
			return new CalendarDate(day,month,year);
		}
		return new CalendarDate(++day,month,year);
	}
	public static int getNoDays(int m){
		
		int no_days = 0;
		
		switch (m){
		case 0: no_days = 31; break;
		case 1: no_days = 28;break;
		case 2: no_days= 31;break;
		case 3: no_days= 30;break;
		case 4: no_days = 31;break;
		case 5: no_days = 30;break;
		case 6: no_days = 31;break;
		case 7: no_days = 31;break;
		case 8: no_days = 30;break;
		case 9: no_days = 31;break;
		case 10: no_days = 30;break;
		case 11: no_days = 31;break;

		default: return -1;

		}

		return no_days;
		
	}
	
	public String toString() {
		// turn the CalendarDate object into a string

		return "" + day + "-"+month+"-"+year;

	}
}
